package com.example.myapplication.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.database.UserDBHelper;


public class MyDBProvider extends ContentProvider {

    private UserDBHelper userDBHelper = null;

    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    @Override
    public boolean onCreate() {
        // 获取数据库实例
        userDBHelper = UserDBHelper.getInstance(getContext());
        return false;
    }

    /**
     * 添加规则
     */
    static {
        uriMatcher.addURI("com.example.myapplication.provider.MyDBProvider",null,1);
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            SQLiteDatabase readableDatabase = userDBHelper.getReadableDatabase();
            return readableDatabase.query("user_info", null, null, null, null, null, null);
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            SQLiteDatabase readableDatabase = userDBHelper.getReadableDatabase();
            readableDatabase.insert("user_info", null, contentValues);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return uri;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            SQLiteDatabase readableDatabase = userDBHelper.getReadableDatabase();
            readableDatabase.delete("user_info",s,strings);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return 1;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            SQLiteDatabase readableDatabase = userDBHelper.getReadableDatabase();
            readableDatabase.update("user_info", contentValues, s, strings);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return 1;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Nullable
    @Override
    public Bundle call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras) {
        return super.call(method, arg, extras);
    }
}